package com.lkd.handler;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.lkd.business.MsgHandler;
import com.lkd.common.VMSystem;
import com.lkd.config.TopicConfig;
import com.lkd.contract.OrderCheck;
import com.lkd.emq.Topic;
import com.lkd.entity.OrderEntity;
import com.lkd.service.OrderService;
import com.lkd.utils.JsonUtil;
import org.elasticsearch.common.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@Topic(TopicConfig.ORDER_CHECK_TOPIC)
public class OrderCheckHandler implements MsgHandler {

    @Autowired
    private OrderService orderService;

    @Override
    public void process(String jsonMsg) throws IOException {
        OrderCheck check = JsonUtil.getByJson(jsonMsg, OrderCheck.class);
        if(check == null || Strings.isNullOrEmpty(check.getOrderNo()))return;

        String orderNo = check.getOrderNo();
        QueryWrapper<OrderEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .lambda()
                .eq(OrderEntity::getOrderNo, orderNo)
                .eq(OrderEntity::getStatus, VMSystem.ORDER_STATUS_CREATE);
        OrderEntity orderEntity = orderService.getOne(queryWrapper);
        if(orderEntity==null) return;

        UpdateWrapper<OrderEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper
                .lambda()
                .eq(OrderEntity::getOrderNo, orderNo)
                .set(OrderEntity::getStatus,VMSystem.ORDER_STATUS_INVALID);
        orderService.update(updateWrapper);
    }
}
